package com.sosee.cgfw.friends;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.sosee.cgfw.R;
import com.sosee.cgfw.utils.Constans;
import com.sosee.cgfw.utils.LogUtils;
import com.sosee.cgfw.utils.Utils;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Thinkpad on 2016/3/1.
 */
public class FriendsTogether extends Activity implements AdapterView.OnItemClickListener {
    private  ListView friends_listtogether;
    private HttpUtils httpUtils;
    private List<TogetherData.Data> data;
    private Context context;
    MyTogetherAdapter myTogetherAdapter;
    List<String> friendsId = new ArrayList<String>();
    List<FriendsListData.Data> datas;



    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_friends_together);

        context=this;

//        base_title.setText("共同好友");
        httpUtils=new HttpUtils();
        init();
    }

    private void init() {

        ImageButton friends_back4 = (ImageButton)findViewById(R.id.friends_back4);
        friends_back4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        friends_listtogether=(ListView) findViewById(R.id.friends_listtogether);
        myTogetherAdapter = new MyTogetherAdapter();
        friends_listtogether.setAdapter(myTogetherAdapter);
        friends_listtogether.setOnItemClickListener(this);
        request();
        requestHttp();

    }

    private void request() {


        httpUtils.send(HttpRequest.HttpMethod.GET, Constans.GETFRIENDSLIST_URL, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String result = responseInfo.result;
                System.out.println("我是好友列表返回的数据：" + result);

                Gson gson = new Gson();
                FriendsListData friendsListData = gson.fromJson(result, FriendsListData.class);
                datas = friendsListData.getData();

                if("success".equals(friendsListData.getStatus())) {
                    for (int i = 0; i < datas.size(); i++) {
                        String ID = datas.get(i).getFriends().getID();
                        //吧名字赋值给name
                        friendsId.add(ID);

                    }
                }else{}


            }

            @Override
            public void onFailure(HttpException e, String s) {
                Utils.showToast("网络加载异常");
            }
        });

    }
    private void requestHttp() {

        httpUtils.send(HttpRequest.HttpMethod.GET, Constans.GETTOGETHER_URL+Constans.userID,new RequestCallBack<String>(){
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String s = responseInfo.result;

                LogUtils.log_i(s);


                Gson gson = new Gson();
                TogetherData togetherData = gson.fromJson(s,TogetherData.class);
                data = togetherData.getData();

                myTogetherAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(HttpException e, String s) {
                Utils.showToast("网络获取异常");
            }
        });
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //
        switch (parent.getId()){
            case R.id.friends_listtogether:
                //这是
                Intent intent = new Intent(FriendsTogether.this,FriendsTogetherInfos.class);
                intent.putExtra("together", data.get(position).getFriends().getID());

                startActivity(intent);
                break;
            default:
                break;
        }
    }

    public class MyTogetherAdapter extends BaseAdapter {

        @Override
        public int getCount() {
//            LogUtils.log_i("看来是走了这一步的0");

            return data==null?0:data.size();


        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
//                LogUtils.log_i("看来是走了这一步的");


            final ViewHolder holder;
            if (convertView == null) {

                holder = new ViewHolder();
                convertView = LayoutInflater.from(context).inflate(R.layout.friends_listnear_item, null);

                if(position%2==0){
                   //这里是改变条目背景色的
//                    convertView.setBackgroundResource(R.color.near);
                    convertView.setBackgroundColor(Color.parseColor("#43343C"));
//                    notifyDataSetChanged();
                }else{
                    convertView.setBackgroundColor(Color.parseColor("#2F2629"));
//                    notifyDataSetChanged();
                }
                holder.friends_name1 = (TextView) convertView.findViewById(R.id.friends_name);
                holder.friends_company1 = (TextView) convertView.findViewById(R.id.friends_company);
                holder.friends_sort1 = (TextView) convertView.findViewById(R.id.friends_sort);
                holder.friends_addstutas1 = (TextView) convertView.findViewById(R.id.friends_addstuta);




                if(holder.friends_addstutas1.getText().equals("加为好友")){
                    holder.friends_addstutas1.setClickable(true);
                    holder.friends_addstutas1.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            //加为好友被点击了就请求网络

                            httpUtils.send(HttpRequest.HttpMethod.GET, Constans.ADDFRIENDS_URL +Constans.userID+ "&friendsID=" + data.get(position).getFriends().getID(), new RequestCallBack<String>() {
                                @Override
                                public void onSuccess(ResponseInfo<String> responseInfo) {
                                    String s = responseInfo.result;
                                    System.out.println("这是返回的值" + s);
                                    //发送消息到主线程通知更改  加为好友为等待验证。
                                    Utils.showToast(s);
                                    holder.friends_addstutas1.setText("等待验证");
//
                                }

                                @Override
                                public void onFailure(HttpException e, String s) {

                                    Utils.showToast("网络获取异常");
                                }
                            });


                        }
                    });

                }





                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.friends_name1.setText(data.get(position).getFriends().getNickname());
            holder.friends_company1.setText(data.get(position).getFriends().getCompany());
            holder.friends_sort1.setText(data.get(position).getFriends().getDuty());
            
            LogUtils.log_i("看来是走了这一步的1position" + position);
            Log.e("hu",convertView.toString());
            return convertView;


        }
        class ViewHolder {
            public TextView friends_name1;
            public TextView friends_company1;
            public TextView friends_sort1;
            public TextView friends_addstutas1;

        }

    }

}
